package com.situ.crm.controller;

import javax.servlet.http.HttpSession;

import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.crm.common.ServerResponse;
import com.situ.crm.entity.Role;
import com.situ.crm.entity.User;
import com.situ.crm.service.IUserService;

@Controller
@RequestMapping(value="/user")
public class UserController {
	@Autowired
	private IUserService userService;
	
	@RequestMapping(value="/getUserPage")
	public String getUserPage() {
		return "user/user_list";
	}
	
	@RequestMapping(value="/pageList")
	@ResponseBody
	public ServerResponse pageList(Integer page,Integer limit,User user) {
		ServerResponse serverResponse = userService.pageList(page,limit,user);
		return serverResponse;
	}
	
	//添加页面
	@RequestMapping(value="/getInsertPage")
	public String getInsertPage() {
		return "user/user_insert";
	}
	
	//添加
	@RequestMapping(value="/insert")
	@ResponseBody
	public ServerResponse insert(User user,String roles) {
		return userService.insert(user,roles);
	}
	
	//删除
	@RequestMapping(value="/deleteById")
	@ResponseBody
	public ServerResponse deleteById(Integer id) {
		return userService.deleteById(id);
	}
	
	//批量删除
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public ServerResponse deleteAll(String ids) {
		return userService.deleteByAll(ids);
	}
	
	//修改页面
	@RequestMapping(value="/getUpdatePage")
	public String getUpdatePage() {
		return "user/user_update";
	}
	
	//修改查询
	@RequestMapping(value="/selectUserAndRole")
	@ResponseBody
	public ServerResponse selectUserAndRole(Integer userId) {
		return userService.selectUserAndRole(userId);
	}
	
	//修改
	@RequestMapping(value="/update")
	@ResponseBody
	public ServerResponse update(User user,String roles) {
		return userService.update(user,roles);
	}
	
	//登陆--------------------------
	@RequestMapping(value="/getLoginPage")
	public String name() {
		return "user/login";
	}
	
	//登陆验证
	@RequestMapping(value="/login")
	@ResponseBody
	public ServerResponse login(String name,String password,HttpSession session) {
		System.err.println(name);
		return  userService.login(name,password,session);
		
		
	}
	
	//验证码验证
	@RequestMapping(value="/YZM")
	@ResponseBody
	public synchronized Boolean input(String checkCode,HttpSession session) {
		/*这里的验证码生成的数字保存在session域中经过login页面直接在这拿出来*/
		String checkCodeSession  = (String)session.getAttribute("checkCodeSession");
		System.out.println(checkCodeSession);
		if (checkCode.equalsIgnoreCase(checkCodeSession)) {
			return true;
		} else {
			return false;
		}
	}
	
	//退出登陆
	@RequestMapping(value="/loginout")
	public String loginout(HttpSession session) {
		session.removeAttribute("UserName");//销毁session
		session.invalidate();
		return "user/login";
	}
	
	
}
